/**
 * Created by lvlq on 16/3/12.
 */
module.exports.index = function (req, res) {
    var openid = req.query.openid;
    var Record = M("index.record");
    var Prize = M("index.prize");
    var zan = {
        "1": {},
        "2": {},
        "3": {}
    };
    //1 jd5 2jd30 3lxx
    var cart = {
        "0": {
            "0": {
                "0": 0,
                "1": 3
            },
            "1": {
                "0": 2,
                "1": 4
            }
        },
        "1": {
            "0": {
                "0": 1,
                "1": 5
            },
            "1": {
                "0": 6,
                "1": 7
            }
        }
    };
    var a = 0, b = 0, c = 0;
    var prize_arr = [];
    var plist;
    var User = M("index.user");
    var userinfo;
    User.find({
        where: {
            openid: openid
        }
    }).then(function (u) {
        userinfo = u;
        return Record.findAll({
            where: {
                openid: openid
            }
        })
    }).then(function (data) {
        if (data.length == 0) {
            res.render("main.html");
        } else {
            Prize.findAll().then(function (prize_list) {
                plist = prize_list;
                for (var i = 0; i < prize_list.length; i++) {
                    zan[prize_list[i].id].all = prize_list[i].zan_num;
                }
                return Record.findAll({
                    where: {
                        openid: openid,
                        pid: 1
                    }
                })
            }).then(function (jd5) {
                zan["1"].num = jd5.length;
                if (zan["1"].num > zan["1"].all)
                    zan["1"].num = zan["1"].all;

                if (zan["1"].num == zan["1"].all) {
                    a = 1;
                    prize_arr.push(plist[0].name);
                }
                return Record.findAll({
                    where: {
                        openid: openid,
                        pid: 2
                    }
                })
            }).then(function (jd30) {
                zan["2"].num = jd30.length;
                if (zan["2"].num > zan["2"].all)
                    zan["2"].num = zan["2"].all;

                if (zan["2"].num == zan["2"].all) {
                    b = 1;
                    prize_arr.push(plist[1].name);
                }
                return Record.findAll({
                    where: {
                        openid: openid,
                        pid: 3
                    }
                });
            }).then(function (lxx) {
                zan["3"].num = lxx.length;
                if (zan["3"].num > zan["3"].all)
                    zan["3"].num = zan["3"].all;

                if (zan["3"].num == zan["3"].all) {
                    c = 1;
                    prize_arr.push(plist[2].name);
                }

                var Result = M("index.result");

                return Result.findAll({
                    where: {
                        openid: openid
                    }
                });

            }).then(function (list) {
                var prize_get = {
                    "1": 0,
                    "2": 0,
                    "3": 0
                };
                var buy_cart = cart[a][b][c];
                for (var i = 0; i < list.length; i++) {
                    prize_get[list[i].pid] = 1;
                }

                res.render("cart.html", {
                    zan: zan,
                    buy_cart: buy_cart,
                    jd5: a,
                    jd30: b,
                    lxx: c,
                    prize_arr: prize_arr,
                    prize_get: prize_get
                });
            })
        }
    }).catch(function (err) {
        res.json({
            errno: 1001,
            errmsg: err.message
        });
    });

};